using Core.Contracts.Interfaces;

namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Resources;
    using System.Linq;

    
    public sealed class CriticalHitChance<T> : FeatureBase<object>
    {
        public CriticalHitChance(){}

        public CriticalHitChance(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Critical hit chance"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.General; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.CriticalHitChance; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_percent; } }

        /// <summary>
        /// Calculate the modified feature value
        /// </summary>
        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (robot == null || agent == null) return;

            // Bonuses from extensions
            double extensionBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Robot, this.FeatureType);

            // Bonuses from robot
            double robotBonus = robot.Modifiers.SumModifiersValues(agent.Extensions, FeatureTypesEnum.Robot, this.FeatureType);

            double temp = extensionBonus + robotBonus;

            temp = robot.Modules.Aggregate(temp, (current, modifier) => current + (Math.Abs(modifier.Value)), FeatureTypesEnum.Robot, this.FeatureType);

            // nexus Bonus
            double nexusBonus = robot.Modules.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType, ModuleGroupsEnum.NexusModuleCriticalHit);
            double nexusSkillBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Module, this.FeatureType);

            double nexusTotalBonus = 0;

            if (nexusBonus != 0)

                nexusTotalBonus = nexusBonus +  nexusSkillBonus;

            temp = temp + nexusTotalBonus;

            ModifiedValue = IsRounded
                ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                : Math.Round(temp, 4);
        }

        public override string FormattedValue
        {
            get { return String.Format("{0} {1}", IsModified ? (double)ModifiedValue * 100 : (double)Value * 100, Unit); }
        }
    }
}